// map.js
let repairId = ''
import qqMap from '../../../utils/qqMap'
Page({
  data: {
    dialogVisible: false,
    latitude: 40.060539,
    longitude: 116.343847,
    markers: [
      // 云趣园
      {
        id: 1,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 24,
        height: 30
      },
      // 育知路
      {
        id: 2,
        latitude: 40.08346500000002,
        longitude: 116.33293800000011,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40
      },
      {
        id: 3,
        latitude: 40.060539,
        longitude: 116.343847,
        width: 40,
        height: 40
      }
    ],
    polyline: [{
      points: [
        { latitude: 40.08346500000002, longitude: 116.33293800000011 }, // 云趣园
        { latitude: 40.086757, longitude: 116.328634 }, // 育知路
      ],
      color: '#999',
      width: 4
    }]
  },
  // 生成路线
  createPolyLine() {
    qqMap.direction({
      mode: 'bicycling',
      from: '40.086757,116.328634',
      to: '40.08346500000002,116.33293800000011',
      success: (res) => {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        this.setData({
          polyline: [
            {
              points: pl,
              color: '#5591af',
              width: 4
            }
          ]
        })
      }
    })
  },
  // 取消报修
  cancelRepair() {
    this.setData({ dialogVisible: true })
  },
  // 跳转编辑页面
  goEditRepair() {
    wx.navigateTo({
      url: `../form/index?id=${repairId}`
    })
  },
  // 确认提示框关闭时触发
  async dialogClose(ev) {
    console.log(ev.detail)
    if (ev.detail === 'confirm') {
      const { code } = await wx.http.put('/cancel/repaire/' + repairId)
      // 检测接口的调用结果
      if (code !== 10000) return wx.utils.toast()
      wx.navigateBack()
    }
  },
  async onLoad({ id }) {
    // 根据id获取详情
    const { code, data } = await wx.http.get(`/repair/${id}`)
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      ...data
    })
    console.log(data, 'data');
    this.createPolyLine()
    repairId = id
  }

})
